
package com.jf.cloud.multishop.controller.admin;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.multishop.constant.HotSearchType;
import com.jf.cloud.multishop.dto.HotSearchDTO;
import com.jf.cloud.multishop.model.HotSearch;
import com.jf.cloud.multishop.service.HotSearchService;
import com.jf.cloud.multishop.vo.HotSearchVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 热搜
 *
 * @author zz
 * @date 2021-01-27 09:10:00
 */
@RestController("adminHotSearchController")
@RequestMapping("/mp/hot_search")
@Tag(name = "admin-热搜")
public class HotSearchController {
    private static final Logger log = LoggerFactory.getLogger(HotSearchController.class);

    @Autowired
    private HotSearchService hotSearchService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "分页获取热搜列表" , description = "分页获取热搜列表")
    public ServerResponseEntity<PageVO<HotSearchVO>> page(@Valid PageDTO pageDTO, @ParameterObject HotSearchDTO hotSearchDTO) {
        hotSearchDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<HotSearchVO> hotSearchPage = hotSearchService.page(pageDTO, hotSearchDTO);
        return ServerResponseEntity.success(hotSearchPage);
    }

    @GetMapping
    @Operation(summary = "获取热搜" , description = "根据hotSearchId获取热搜")
    public ServerResponseEntity<HotSearchVO> getByHotSearchId(@ParameterObject @RequestParam Long hotSearchId) {
        return ServerResponseEntity.success(hotSearchService.getByHotSearchId(hotSearchId));
    }

    @PostMapping
    @Operation(summary = "保存热搜" , description = "保存热搜")
    public ServerResponseEntity<Void> save(@Valid @RequestBody HotSearchDTO hotSearchDTO) {
        HotSearch hotSearch = mapperFacade.map(hotSearchDTO, HotSearch.class);
        hotSearch.setShopId(AuthUserContext.get().getTenantId());
        if (Objects.isNull(hotSearch.getType())) {
            hotSearch.setType(HotSearchType.PROD.value());
        }
        // 热搜标题不能重复
        if (hotSearchService.checkExist(hotSearch.getShopId(), hotSearch.getTitle(), hotSearch.getType())) {
            log.error("热搜标题不能重复");
            throw new LuckException("热搜标题不能重复");
        }
        hotSearchService.save(hotSearch);
        hotSearchService.removeHotSearchListCache(hotSearch.getShopId(), hotSearch.getType());
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新热搜" , description = "更新热搜")
    public ServerResponseEntity<Void> update(@Valid @RequestBody HotSearchDTO hotSearchDTO) {
        HotSearch hotSearch = mapperFacade.map(hotSearchDTO, HotSearch.class);
        HotSearchVO dbHotSearch = hotSearchService.getByHotSearchId(hotSearchDTO.getHotSearchId());
        if (Objects.isNull(dbHotSearch)) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        }
        hotSearch.setShopId(AuthUserContext.get().getTenantId());
        if (!Objects.equals(dbHotSearch.getShopId(), hotSearch.getShopId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        hotSearchService.update(hotSearch);
        hotSearchService.removeHotSearchListCache(hotSearch.getShopId(), dbHotSearch.getType());
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除热搜" , description = "根据热搜id删除热搜")
    public ServerResponseEntity<Void> delete(@RequestParam Long hotSearchId) {
        HotSearchVO dbHotSearch = hotSearchService.getByHotSearchId(hotSearchId);
        if (Objects.isNull(dbHotSearch)) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        }
        if (!Objects.equals(dbHotSearch.getShopId(), AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        Long shopId = AuthUserContext.get().getTenantId();
        hotSearchService.deleteById(hotSearchId, shopId);
        hotSearchService.removeHotSearchListCache(shopId, dbHotSearch.getType());
        return ServerResponseEntity.success();
    }
}
